package com.atguigu.yygh.cmn.controller;


import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-05-19
 */
@Api(value = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    @ApiOperation(value = "查询子节点元素")
    @GetMapping(value = "/childList/{pid}")
    public R getChildListByPid(@PathVariable Long pid) {
        List<Dict> list = dictService.getChildListByPid(pid);
//        System.out.println("list = " + list);
        return R.ok().data("item", list);
    }

    @ApiOperation(value = "导出数据字典excel文件")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) throws IOException {
        dictService.exportData(response);
    }

    @ApiOperation(value = "上传数据excel文件")
    @PostMapping(value = "/importData")
    public R upload(MultipartFile file) {
        dictService.importDictData(file);
        return R.ok();
    }

    @ApiOperation(value = "根据医院所属得省市区编号获取等级信息")
    @GetMapping(value = "/{value}")
    public String getNameByValue(@PathVariable(value = "value") Long value) {
        return dictService.getNameByValue(value);
    }

    @ApiOperation(value = "根据医院等级编号获取等级信息")
    @GetMapping(value = "/{dictCode}/{value}")
    public String getNameByValueAndDictCode(
            @PathVariable(value = "value") Long value,
            @PathVariable(value = "dictCode") String dictCode) {
        return dictService.getNameByValueAndDictCode(dictCode,value);
    }

}

